import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:wallet/common/common.dart';
import 'package:wallet/generated/l10n.dart';
import 'package:wallet/pages/mine/settings/change_password_page.dart';
import 'package:wallet/pages/mine/settings/language_page.dart';
import 'package:wallet/pages/mine/settings/node_page.dart';
import 'package:wallet/widget/BaseViewBar.dart';

class SettingsPage extends StatefulWidget {
  @override
  _SettingsPageState createState() => _SettingsPageState();
}

class _SettingsPageState extends State<SettingsPage> {
  Future<void> _showCleanCacheDialog() async {
    return showCupertinoDialog(
      context: context,
      builder: (BuildContext context) {
        return CupertinoAlertDialog(
          title: Text('温馨提示'),
          content: SingleChildScrollView(
            child: Text('是否清理缓存？'),
          ),
          actions: <Widget>[
            CupertinoDialogAction(
              child: Text('确定'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            CupertinoDialogAction(
              child: Text('取消'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }

  List<Item> getList(BuildContext context) {
    return [
      Item(S.of(context).language_settings, onTap: () {
        Navigator.of(context).push(MaterialPageRoute(
            builder: (BuildContext context) => LanguagePage()));
      }),
      Item(S.of(context).node_settings, onTap: () {
        Navigator.of(context).push(
            MaterialPageRoute(builder: (BuildContext context) => NodePage()));
      }),
      Item(S.of(context).version_detection, onTap: () {}),
      Item(S.of(context).change_password, onTap: () {
        Navigator.of(context).push(MaterialPageRoute(
            builder: (BuildContext context) => ChangePasswordPage()));
      }),
      Item(S.of(context).fingerprint, onTap: () {}),
      Item(S.of(context).apply_lock, onTap: () {}),
      Item(S.of(context).clean_cache, onTap: () {
        _showCleanCacheDialog();
      }),
    ];
  }

  Widget _itemWidget(BuildContext context, int index) {
    return Container(
      decoration: BoxDecoration(
          border: Border(bottom: BorderSide(width: 1, color: Colors.black12))),
      child: ListTile(
          title: Text(getList(context)[index].title),
          trailing: index == 4 || index == 5
              ? Switch(
                  value: false, onChanged: getList(context)[index].onChanged)
              : Icon(Icons.arrow_forward_ios),
          onTap: getList(context)[index].onTap),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: BaseViewBar(
        preferredSize: Size.fromHeight(50.0),
        child: BaseTitleBar(
          S.of(context).system_settings,
        ),
      ),
      body: ListView.builder(
          itemCount: getList(context).length, itemBuilder: (context, index) {
        return _itemWidget(context, index);
      }),
    );
  }
}
